package com.cskaoyan.service;

import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.KeywordExample;
import com.cskaoyan.bean.SearchHistory;
import com.cskaoyan.bean.wxsearch.WxSearchIndexData;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: yudefu
 * @date 2021年06月07日  23:39
 * @description: 微信搜索模块
 */
@Service
public class WxSearchServiceImpl implements WxSearchService{

    @Autowired
    KeywordMapper keywordMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public WxSearchIndexData getSearchIndex() {
        WxSearchIndexData wxSearchIndexData = new WxSearchIndexData();

        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria keywordCriteria = keywordExample.createCriteria();
        keywordCriteria.andDeletedEqualTo(false);
        keywordCriteria.andIsDefaultEqualTo(true);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        wxSearchIndexData.setDefaultKeyword(keywords.get(0));


        KeywordExample hotKeywordExample = new KeywordExample();
        KeywordExample.Criteria hotCriteria = hotKeywordExample.createCriteria();
        hotCriteria.andDeletedEqualTo(false);
        hotCriteria.andIsHotEqualTo(true);
        List<Keyword> hotKeywordList = keywordMapper.selectByExample(hotKeywordExample);
        wxSearchIndexData.setHotKeywordList(hotKeywordList);


        List<SearchHistory> searchHistoryList = searchHistoryMapper.selectHistoryList();
        wxSearchIndexData.setHistoryKeywordList(searchHistoryList);

        return wxSearchIndexData;
    }

    @Override
    public List<String> searchHelper(String keyword) {

        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andKeywordLike("%" + keyword + "%");

        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        List<String> keyLists = new ArrayList<>();

        for (Keyword key : keywords) {

            String returnKey = key.getKeyword();
            keyLists.add(returnKey);
        }

        return keyLists;
    }

    @Override
    public int clearSearchHistory() {

        int affectRows = searchHistoryMapper.clearSearchHistory(true);

        return affectRows;
    }
}
